package com.roy.sms.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.roy.sms.service.ArticleService;
import com.roy.sms.util.PageParam;
import com.roy.sms.util.Pager;
import com.roy.sms.vo.ArticleVO;

@Controller
public class ArticleController extends BaseController{

	@Resource
	private ArticleService articleService;
	
	@RequestMapping("/article/writeArticle.do")
	public String writeArticle(){
		return "article/writeArticle";
	}
	
	@RequestMapping("/article/manager.do")
	public String managerArticle(){
		return "article/articleManager";
	}
	
	@RequestMapping("/article/saveArticle.do")
	@ResponseBody
	public String saveArticle(ArticleVO articleVO){
		articleService.add(articleVO);
		return SUCCESS;
	}
	
	@RequestMapping("/article/deleteArticles.do")
	@ResponseBody
	public String deleteArticles(String ids){
		articleService.delete(ids);
		return SUCCESS;
	}
	
	@RequestMapping("/article/listArticle.do")
	@ResponseBody
	public Pager<ArticleVO> list(PageParam param){
		return articleService.list(param.getRowBeginIndex(), param.getRowCount(), null);
	}
	
	@RequestMapping("/article/publish.do")
	@ResponseBody
	public String publish(int act, String ids){
		articleService.publish(act, ids);
		return SUCCESS;
	}
	
	@RequestMapping("/article/view.do")
	public String viewArticle(int id,ModelMap map, boolean edit){
		map.put("articleVO", articleService.loadArticle(id));
		map.put("edit", edit);
		return "article/viewArticle";
	}
	
	@RequestMapping("/article/initEdit.do")
	public String initEdit(int id,ModelMap map){
		map.put("articleVO", articleService.loadArticle(id));
		return "article/editArticle";
	}
	
	@RequestMapping("/article/saveEdit.do")
	@ResponseBody
	public String saveUpdate(ArticleVO article){
		ArticleVO articleVO = articleService.loadArticle(article.getId());
		articleVO.setTitle(article.getTitle());
		articleVO.setContent(article.getContent());
		articleVO.setColumnId(article.getColumnId());
		
		articleService.update(articleVO);
		return SUCCESS;
	}
}
